﻿using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace nEditor
{
    public partial class ZoneMenu : EditorWindow
    {
        private static GUIContent
            ButtonContent = new GUIContent("创建", "generate map data");


        private static GUIContent
            LoadButtonContent = new GUIContent("加载", "load map file");

        ZoneGenerator zonegenrator = new ZoneGenerator();

        [MenuItem(@"XEditor/Zone %k")]
        static void Zone()
        {
            ZoneMenu window = (ZoneMenu)EditorWindow.GetWindow(typeof(ZoneMenu), true, "本阵编辑器");
            window.Show();
        }

        public int toolbarOption = 0;
        public string[] toolbarTexts = { "地图编辑", "单位的CSV转Json", "行为树转CSV", "(待续)" };


        public void OnGUI()
        {
            toolbarOption = GUILayout.Toolbar(toolbarOption, toolbarTexts, GUILayout.Height(30));

            GUILayout.Space(10f);

            switch (toolbarOption)
            {
                case 0:
                    ZoneFrame();
                    ZoneMap();
                    break;
                case 1:
                    ZoneFrameUnitcsv2json();
                    break;
                case 2:
                    ZoneFrameBt2CSV();
                    break;
                case 3:
                    break;
            }

        }

        int _width_cell = 0;
        int _height_cell = 0;

        string sWidth = "0";
        string sHeight = "0";
        void ZoneFrame()
        {
            GUILayout.Label("宽单元格(整数): ");
            sWidth = GUILayout.TextField(sWidth.ToString());

            GUILayout.Label("高单元格(整数): ");
            sHeight = GUILayout.TextField(sHeight.ToString());

            return;
        }

        bool IsCanZoneMap()
        {
            if (int.TryParse(sWidth, out _width_cell))
            {
                zonegenrator._Groud._Width = _width_cell * ZoneCfg.RhombusMin;
            }
            else
            {
                Debug.LogError("宽 " + sWidth);
                return false;
            }


            GUILayout.Label("高单元格(整数): ");
            if (int.TryParse(sHeight, out _height_cell))
            {
                zonegenrator._Groud._Height = _height_cell * ZoneCfg.RhombusMin / ZoneCfg.RhombusFactor;
            }
            else
            {
                Debug.LogError("高" + sHeight);
                return false;
            }

            return true;
        }

        private void ZoneMap()
        {
            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button(ButtonContent))
            {
                if (IsCanZoneMap())
                {
                    if (zonegenrator._Groud._Width < ZoneCfg.RhombusMin)
                        zonegenrator._Groud._Width = ZoneCfg.RhombusMin;

                    if (zonegenrator._Groud._Height < ZoneCfg.RhombusMin / ZoneCfg.RhombusFactor)
                        zonegenrator._Groud._Height = ZoneCfg.RhombusMin / ZoneCfg.RhombusFactor;


                    GenerateMapData();
                }
            }

            if (GUILayout.Button(LoadButtonContent))
            {
                LoadMapHeight();
            }

            if (GUILayout.Button("Reset"))
            {
                zonegenrator.Reset();
            }
            EditorGUILayout.EndHorizontal();
        }

        private void GenerateMapData()
        {
            if (zonegenrator.SeriZoneMap(   (int)zonegenrator._Groud._Width,
                                            (int)zonegenrator._Groud._Height))
            {
                string path = EditorUtility.SaveFilePanel("Select a file to save", ZoneConfig.MapPath, Time.realtimeSinceStartup.ToString() + "_" + sWidth + " x " + sHeight + "_zone.txt", "txt");

                if (path != null && path != "")
                    zonegenrator.Generate(path);

            }

            AssetDatabase.Refresh();

        }

        private void LoadMapHeight()
        {
            SpriteRenderer[] allObjs = GameObject.FindObjectsOfType<SpriteRenderer>();
            if (allObjs != null)
            {
                for (int i = 0; i < allObjs.Length; ++i)
                {
                    SpriteRenderer sr = allObjs[i];

                    GameObject.DestroyImmediate(sr.gameObject);
                }

            }

            string path = EditorUtility.OpenFilePanel("Select a file to load", ZoneConfig.MapPath, "txt");

            if (path != null && path != "")
            {
                zonegenrator.LoadFromFile(path);

                ZonePresentEditor zpe = new ZonePresentEditor();
                zpe.LoadFromFile(path);
            }

            Scene scene = EditorSceneManager.GetActiveScene();

            EditorSceneManager.MarkSceneDirty(scene);
        }

    }
}
